

import 'package:flutter/material.dart';
import 'package:syncfusion_flutter_datagrid/datagrid.dart';
import 'package:xsdk_flutter/xsdk/framework/base/xsdk_function.dart';
import 'package:xsdk_flutter/xsdk/utils/x_utils_widget.dart';

/*
  类似DataTable
  https://pub-web.flutter-io.cn/packages/syncfusion_flutter_datagrid
  dependencies:
    syncfusion_flutter_datagrid: ^21.2.9
  import 'package:syncfusion_flutter_datagrid/datagrid.dart';
*/


//组装Row数据
class XSfDataGridRowData{

  List<XSfDataGridCell> cells;
  Color? rowColor;

  XSfDataGridRowData(
      {
        required this.cells,
        this.rowColor,
      });
}


//列表头
class XSfDataGridColumn{

  String columnName;//key
  String? valueShow; //show

  double? columnWidth;//列宽,用于手动拖拉改变列宽
  double? columnMinWidth;
  double? columnMaxWidth;

  Color backgroundColor;
  TextStyle textStyle;
  int textMaxLines=1;
  Alignment alignment;
  Widget? child;

  bool isSortable;//是否排序
  String? currentSortImage;//当前排序图标
  double currentSortImageWidth=6;
  double currentSortImageHeight=12;

  XSfDataGridColumn(
      {
        required this.columnName,
        this.valueShow,
        this.backgroundColor=Colors.white,
        this.textStyle=const TextStyle(color:Color(0xFF999999),fontSize: 12),
        this.textMaxLines=1,
        this.alignment=Alignment.center,
        this.child,
        this.isSortable=false,
        this.currentSortImage,
        this.currentSortImageWidth=6,
        this.currentSortImageHeight=12,
        this.columnWidth,
        this.columnMinWidth,
        this.columnMaxWidth,
      }
      );

}


//单元cell
class XSfDataGridCell extends DataGridCell{

  Color backgroundColor;
  TextStyle textStyle;
  int textMaxLines=1;
  Alignment alignment;
  Widget? child;

  XSfDataGridCell(
      {required super.columnName,
        required super.value,
        this.backgroundColor=Colors.transparent,
        this.textStyle=const TextStyle(color:Color(0xFF333333),fontSize: 14,),
        this.textMaxLines=1,
        this.alignment=Alignment.center,
        this.child,
      }
      );
}

//row
class XSfDataGridRow extends DataGridRow{
  Color? rowColor;

  XSfDataGridRow(
      {
        required super.cells,
        this.rowColor, //每行颜色
      }
      );
}


//数据源
class XSfDataSource extends DataGridSource {

  XFunctionSfDataGridRowItem rowItemBuildFunction;
  XFunctionSfDataGridLoadMoreData? loadMoreDataFunction;
  List<dynamic> data;

  XSfDataSource(
      {
        required this.data,
        required this.rowItemBuildFunction,
        this.loadMoreDataFunction,
      }
      );

  //List<XSfDataGridRow> rowData=[];
  //重写获得指定Row数据
  @override
  //List<DataGridRow> get rows => rowData;
  List<DataGridRow> get rows => data.map((dynamic item){
    //
    XSfDataGridRowData rowItemData=rowItemBuildFunction(item);
    //
    return XSfDataGridRow(cells: rowItemData.cells,rowColor: rowItemData.rowColor);
  }).toList();


  @override
  DataGridRowAdapter? buildRow(DataGridRow row) {

    //
    XSfDataGridRow rowItem=row as XSfDataGridRow;

    return DataGridRowAdapter(
      color: rowItem.rowColor,
      cells: rowItem.getCells().map<Widget>((DataGridCell item) {
        //
        XSfDataGridCell cellItem=item as XSfDataGridCell;
        //
        TextAlign textAlign;

        if(cellItem.alignment==Alignment.center){
          textAlign=TextAlign.center;

        }else if(cellItem.alignment==Alignment.centerRight
            || cellItem.alignment==Alignment.bottomRight
            || cellItem.alignment==Alignment.topRight
        ){
          textAlign=TextAlign.end;

        }else{
          textAlign=TextAlign.start;
        }


        return cellItem.child!=null?cellItem.child!:XWidgetUtils.getLayoutContainer(
          backgroundColor: cellItem.backgroundColor,
          alignment: cellItem.alignment,
          child: XWidgetUtils.getWidgetText(
            '${cellItem.value}',
            style: cellItem.textStyle,
            maxLines: cellItem.textMaxLines,
            textAlign: textAlign,
            padding: XWidgetUtils.getEdgeInsets(2, 0, 2, 0)
          ),
        );
      }).toList(),
    );
  }


  @override
  Future<void> handleLoadMoreRows() async{
    //在这里处理加载更多
    if(loadMoreDataFunction!=null){
      loadMoreDataFunction!(this);
    }
  }

  //更新数据,用于替代setState()
  void updatePageData(
      bool isFirstPage,
      List<dynamic> newData,
      {
        int? insertIndex
      }
      ){

    if(isFirstPage){
      data.clear();
    }

    if(insertIndex==null){
      data.addAll(newData);
    }else{
      data.insertAll(insertIndex, newData);
    }

    //更新
    updateWidget();
  }

  void updateWidget(){
    //通知更新,注意:实测使用notifyListeners();有问题,不能更新ui,改为用sort()
    sort();
  }

}